package org.example.demo11;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;

/*
* 11.将一个Person对象写入到文件
class Person{

	private String name;
	private Integer age;

	private Double salary;
* */
public class ObjectToFile {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        FileOutputStream fileOutputStream = null;
        ObjectOutputStream objectOutputStream = null;
        HttpURLConnection
        try {
            File file = new File("E:/object.txt");
            if (!file.exists()) {
                file.createNewFile();
            }
            fileOutputStream = new FileOutputStream(file);
            objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(new Person("aa", 1, 2000.0));
            fileOutputStream.close();
            objectOutputStream.close();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
                objectOutputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        /**
         * 12.读取文件的内容并反序列化改Person实例
        */
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("E:/object.txt"));
        Person person = (Person) objectInputStream.readObject();
        System.out.println(person);
    }
}
